package cn.northsea.controller;

import cn.northsea.pojo.LogisticsVo;
import cn.northsea.pojo.Order;
import cn.northsea.pojo.OrderVo;
import cn.northsea.pojo.TransportPersonnel;
import cn.northsea.service.LogisticsService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;

@Controller
@RequestMapping("/logistics")
public class LogisticsController {
    @Autowired
    private LogisticsService logisticsService;
    @RequestMapping("/findAll")
    public String findAll (@RequestParam(required = false, defaultValue = "1") Integer pageNumA,@RequestParam(required = false, defaultValue = "1") Integer pageNumB,
                           @RequestParam(required = false, defaultValue = "3") Integer pageSize,
                           String likename,
                           Model model){
        PageHelper.startPage(pageNumA, pageSize);
        List<LogisticsVo> logistics = logisticsService.logistics(likename);
        PageInfo<LogisticsVo>   userPageInfo = new PageInfo<>(logistics);
        PageHelper.startPage(pageNumB, pageSize);
        List<Order> orders = logisticsService.Order(likename);
        PageInfo<Order>   pageOrders = new PageInfo<>(orders);
        model.addAttribute("likename", likename);
        model.addAttribute("Page", userPageInfo);
        model.addAttribute("PageB", pageOrders);
        return "logisticsList";

    }

    @RequestMapping("/updateStatu")
    public String updateStatu (@RequestParam("pageNum")String pageNum,@RequestParam("logisticsId") String logisticsId,@RequestParam("number") String number){

        logisticsService.updateStatu(Integer.parseInt(logisticsId),Integer.parseInt(number));

        return "redirect:/logistics/findAll.do?pageNumA="+pageNum;

    }
    @RequestMapping("/updateStatuArr")
    public String updateStatuArr (@RequestParam("pageNum")String pageNum,@RequestParam("ids") String[] ids ){
        logisticsService.updateStatuArr(ids);

        return "redirect:/logistics/findAll.do?pageNumA="+pageNum;

    }

    @RequestMapping("/tanchuang")
    public String tanchuang (Integer id,Model model){
     LogisticsVo tanchuang= logisticsService.tanchuang(id);
       List<TransportPersonnel>   driver= logisticsService.driver();
        model.addAttribute("Logis",tanchuang);
        model.addAttribute("driver",driver);
        return "logisticsListid";

    }

    @RequestMapping("/findAll3")
    public String findAll3 (Model model){
        List<LogisticsVo> logistics = logisticsService.findAll3();
        model.addAttribute("Page", logistics);
        return "logisticsList3";

    }

    @RequestMapping("/updateLogistics")
    public String updateLogistics (LogisticsVo logisticsVo){
        logisticsService.updateLogistics(logisticsVo);
        return "ok";

    }

    @RequestMapping("/generate")
    public String generate (Integer id,Model model){
      OrderVo  orderVo= logisticsService.generate(id);
        List<TransportPersonnel>   driver= logisticsService.driver();
        model.addAttribute("driver",driver);
      model.addAttribute("order",orderVo);
        return "generate";

    }

    @RequestMapping("/addlogistics")
    public String addlogistics (LogisticsVo logisticsVo){
        logisticsService.addlogistics(logisticsVo);
        return "ok";

    }
}
